package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type LoginController struct {
	beego.Controller
}

/**
当为登录状态是，清除cookie
 */
func (this *LoginController) Get()  {
	//清除cookie
	isExit := this.Input().Get("exit")  == "true"
	if isExit{
		//-1是为清除cookie
		this.Ctx.SetCookie("username","",-1,"/")
		this.Ctx.SetCookie("password","",-1,"/")
		this.Redirect("/",301)
		return
	}
	this.TplName = "login.html"
}

/**
将cookie传到服务器中
 */
func (this *LoginController) Post()  {
	//将表单中的内容输出网页中
  //this.Ctx.WriteString(fmt.Sprint(this.Input()))
  //return
  username := this.Input().Get("username")
  password := this.Input().Get("password")
  autoLogin := this.Input().Get("autoLogin") == "on"

  if beego.AppConfig.String("username") == username &&
  	beego.AppConfig.String("password")==password{
	  maxAge := 1

	  if autoLogin{
		  maxAge = 1<<31 - 1
	  }

	  this.Ctx.SetCookie("username",username,maxAge,"/")
	  this.Ctx.SetCookie("password",password,maxAge,"/")
  }

  this.Redirect("/",301)
  return
}
/**
获取cookie并做二次判断
 */
func checkAccount(ctx *context.Context ) bool {
    ck,err := ctx.Request.Cookie("username")
	if err != nil{
		return false
	}
	username := ck.Value

	ck,err = ctx.Request.Cookie("password")
	if err != nil{
		return false
	}
	password := ck.Value

	return beego.AppConfig.String("username") == username &&
		beego.AppConfig.String("password") == password
}
